﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.WebApp
{
    public partial class VoteWebMasterRequests : System.Web.UI.Page
    {
        private const int RequestsByPage = 10;
        private Collection<Model.WebmasterRequest> _requests;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            if (WebmasterManager.IsWebmaster(User.Identity.Name) && WebmasterDataAccess.GetWebmasterByUserId(UserDataAccess.GetUserByEmail(User.Identity.Name).Id).CanSetWebmasters)
            {
                FillDataLists();
                MultiViewCanVoteRequests.ActiveViewIndex = 0;
            }
            else
            {
                if (WebmasterManager.IsWebmaster(User.Identity.Name))
                    MultiViewCanVoteRequests.ActiveViewIndex = 1;
                else
                {
                    Response.Write(
                            "<script>if(window.history.length>1){history.back();} else {window.location.href = 'Default.aspx';}</script>");
                }
            }
        }

        private void FillDataLists()
        {
            _requests = WebmasterRequestDataAccess.GatherAllVotingRequests();
            if (_requests == null)
                MultiViewRequests.ActiveViewIndex = 1;
            else
            {
                if (_requests.Count < 0)
                    MultiViewRequests.ActiveViewIndex = 1;
                else
                {
                    if (!IsPostBack)
                    {
                        Session["NumPage"] = 1;
                        ChangeRequestsPage();
                    }
                    MultiViewRequests.ActiveViewIndex = 0;
                }
            }
        }

        private void ChangeRequestsPage()
        {
            PrevButton.Enabled = true;
            NextButton.Enabled = true;
            int max = _requests.Count;
            int numPage = int.Parse(Session["NumPage"].ToString());
            if (numPage == 1)
                PrevButton.Enabled = false;
            if ((max - ((numPage - 1) * RequestsByPage)) < RequestsByPage)
                NextButton.Enabled = false;
            ShowContent.InnerText = "Showing " + (((numPage - 1) * RequestsByPage) + 1) + "-" + ((((numPage) * RequestsByPage) > max) ? max : ((numPage) * RequestsByPage)) + " of " + max;
            if (_requests.Count == 0)
            {
                MultiViewRequests.ActiveViewIndex = 1;
            }
            else
            {
                Collection<Model.WebmasterRequest> actual = new Collection<Model.WebmasterRequest>();
                int min = (numPage - 1) * RequestsByPage;
                int maxElem = (numPage * RequestsByPage) - 1;
                for (int i = 0; i <= maxElem && i < _requests.Count; i++)
                {
                    if (i >= min && i <= maxElem)
                    {
                        actual.Add(_requests[i]);
                    }
                }
                dataListRequests.DataSource = actual;
                dataListRequests.DataBind();
                MultiViewRequests.ActiveViewIndex = 0;
            }
        }

        private bool bSwitch = false;
        string color1 = "#ffffcc";
        string color2 = "#ccff99";

        public string TRJavaScript(Control con)
        {
            string tmp;
            DataListItem dli = con as DataListItem;
            Button btn = dli.FindControl("HiddenButton") as Button;
            string _js = "bgcolor={0} onMouseover='rowcolor=this" +
                         ".style.backgroundColor;this.style.backgroundColor" +
                         "=\"#B9B9B9\"; this.style.color=\"white\"; this.style.cursor = \"hand\"' " +
                         "onMouseout='this.style.backgroundColor=\"white\"; this.style.color=\"black\";' " +
                         " onclick='document.getElementById(\"{1}\").click();' ";
            tmp = bSwitch ? string.Format(_js, color1, btn.ClientID) :
                           string.Format(_js, color2, btn.ClientID);
            bSwitch = !bSwitch;
            return tmp;
        }

        protected void DL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            long reqId = long.Parse(e.CommandArgument.ToString(), CultureInfo.InvariantCulture);
            Response.Redirect("ViewWebmasterRequest.aspx?reqId=" + reqId);
        }

        protected void PrevButton_Click(object sender, EventArgs e)
        {
            int pageNum = int.Parse(Session["NumPage"].ToString());
            pageNum--;
            Session["NumPage"] = pageNum;
            ChangeRequestsPage();
        }

        protected void NextButton_Click(object sender, EventArgs e)
        {
            int pageNum = int.Parse(Session["NumPage"].ToString());
            pageNum++;
            Session["NumPage"] = pageNum;
            ChangeRequestsPage();
        }

        public string GetUser(object userIdOb)
        {
            long userId = long.Parse(userIdOb.ToString());
            return UserDataAccess.GetUserById(userId).ContactInformation.Email;
        }

        public string GetLinkUser(object userIdOb)
        {
            long userId = long.Parse(userIdOb.ToString());
            return "UserProfile.aspx?user=" + UserDataAccess.GetUserById(userId).ContactInformation.Email;
        }
    }
}